package com.ajie.controller;


import com.ajie.entity.Record;
import com.ajie.entity.Relation;
import com.ajie.entity.SysUser;
import com.ajie.service.ChatService;
import com.ajie.service.MiniService;
import com.ajie.service.SysUserService;
import com.ajie.utils.Result;
import com.ajie.utils.StringUtil;
import com.ajie.vo.MiniUser;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;


@Api(tags = "小程序相关接口")
@RestController
@Slf4j
@RequestMapping("/mini")
public class MiniController {
    @ApiModelProperty(value = "appid")
    @Value("${mini.appid}")
    private String appid;
    
    @ApiModelProperty(value = "secret")
    @Value("${mini.secret}")
    private String secret;
    
    @Autowired
    private SysUserService userService;
    
    @Autowired
    private MiniService miniService;
    
    @Autowired
    private ChatService chatService;
    
    @ApiOperation(value="微信小程序登录")
    @GetMapping("/login")
    public Result login(String code) throws IOException {
        log.info("该文件名是：MiniController,该方法是：findComment");
        if (StringUtil.isEmpty(code)){
            return Result.fail("登录失败，请联系管理员");
        }
        CloseableHttpClient client = HttpClientBuilder.create().build();
        String url = "https://api.weixin.qq.com/sns/jscode2session?"+"appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
        HttpGet get = new HttpGet(url);
        //发送请求
        CloseableHttpResponse response = client.execute(get);
        log.info("请求响应码：{}",response.getStatusLine().getStatusCode());
        String result = EntityUtils.toString(response.getEntity());
        log.info("请求响应结果，-->{}",result);
        JSONObject object = JSON.parseObject(result);
        String openId = object.getString("openid");
        log.info("微信小程序唯一标志 -->{}",openId);
        return miniService.miniLogin(openId);
    }
    
    @ApiOperation(value="微信小程序注册")
    @GetMapping("/register")
    public Result register(String openId) {
        return miniService.miniRegister(openId);
    }
    
    @ApiOperation(value="测试token是否过期")
    @GetMapping("/testToken")
    public Result testToken(){
        
        return Result.success("token没过期");
    }
    
    @ApiOperation(value="更新用户信息")
    @PostMapping("/update/info")
    public Result updateUserInfo(@RequestBody SysUser user){
        log.info("该文件名是：MiniController,该方法是：updateUserInfo");
        return  miniService.updateByOpenId(user);
    }
    
    
    @ApiOperation(value="查找用户信息")
    @PostMapping("/findByUsername")
    public Result findUserInfo(@RequestBody String username){
        log.info("该文件名是：MiniController,该方法是：findUserInfo");
        return  userService.findByUserName(username);
    }
    
    @ApiOperation(value = "申请添加好友")
    @PostMapping("/addFriend")
    public Result addFriend(@RequestBody Relation relation){
        log.info("该文件名是：MiniController,该方法是：addFriend");
        return chatService.addFriend(relation);
    }
    
    @ApiOperation(value = "获取新朋友列表")
    @GetMapping("/findNewFriend/{id}")
    public Result findNewFriend(@PathVariable("id") Long id){
        log.info("该文件名是：MiniController,该方法是：findNewFriend");
        return chatService.findNewFriend(id);
    }
    
    @ApiOperation(value = "同意添加好友")
    @GetMapping("/agreeNewFriend/{id}")
    public Result agreeNewFriend(@PathVariable("id") Long id) {
        log.info("该文件名是：MiniController,该方法是：agreeNewFriend");
        return chatService.agreeNewFriend(id);
    }
    
    @ApiOperation(value = "获取我的朋友列表")
    @GetMapping("/findMyFriend/{id}")
    public Result findMyFriend(@PathVariable("id") Long id){
        log.info("该文件名是：MiniController,该方法是：findMyFriend");
        return chatService.findMyFriend(id);
    }
    
    @ApiOperation(value = "给朋友发送消息")
    @PostMapping("/addRecord")
    public Result addRecord(@RequestBody Record record){
        log.info("该文件名是：MiniController,该方法是：addRecord");
        return chatService.addRecord(record);
    }
    
    @ApiOperation(value = "获取聊天记录")
    @GetMapping("/findRecords/{id}")
    public Result findRecords(@PathVariable("id") Long id){
        log.info("该文件名是：MiniController,该方法是：findRecords");
        return chatService.findRecords(id);
    }
    
    
}
